/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-10-23         luwei
 */

package com.tianci.appstore.ui.widget;

import android.content.Context;
import android.view.View;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.audio.IAudioMatchListener;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.mainpage.MainCgyBlock;
import com.tianci.appstore.ui.widget.BlockFactory.BlockType;
import com.tianci.appstore.ui.widget.SkyBaseItemView.OnAppKeyListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SkyGridAdapter extends SkyBaseAdapter {
    public enum AdapterType {
        MAIN_TYPE,
        MANAGE_TYPE,
        LIST_TYPE
    }

    ;
    private AdapterType type;
    protected List<SkyAppBaseData> mAppList = new ArrayList<SkyAppBaseData>();
    protected Context mContext = null;
    private int mIndex = 0;
    protected int count = 0;
    protected OnAppKeyListener mKeyListener = null;
    //    protected OnAppItemClickListener mClickListener = null;
    protected List<SkyBaseItemView> mAppItemList = new ArrayList<SkyBaseItemView>();
    protected IAudioMatchListener mAudioMatchListener = null;

    //    private float mDiv = 1.0f;
    public SkyGridAdapter(Context context, float div, AdapterType type) {
        this.mContext = context;
//        this.mDiv = div;
        this.type = type;
    }

    public void setKeyListener(OnAppKeyListener listener) {
        this.mKeyListener = listener;
    }

//    public void setClickListener(OnAppItemClickListener listener)
//    {
//        this.mClickListener = listener;
//    }

    public void setAudioMatchListener(IAudioMatchListener listener) {
        this.mAudioMatchListener = listener;
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public int getPosition() {
        return mIndex;
    }

    protected boolean isChildrenMode = false;

    public void setChildrenMode(boolean childrenMode) {
        this.isChildrenMode = childrenMode;
    }

    @Override
    public View getView(int position, View convertView, boolean[] turnpage) {
        SkyBaseItemView appView = null;
        if (convertView == null) {
            BlockType blockType = BlockType.SND_CGY_BLOCK;
            switch (type) {
                case MAIN_TYPE:
                    if (position >= 3 && position <= 5)
                        blockType = BlockType.BIG_RMD_BLOCK;
                    else if (position >= 6)
                        blockType = BlockType.SMALL_RMD_BLOCK;
                    break;
                case MANAGE_TYPE:
                    blockType = BlockType.FUNC_BLOCK;
                    break;
                case LIST_TYPE:
                    blockType = BlockType.LIST_BLOCK;
                    break;
            }
            appView = BlockFactory.createBlock(mContext, blockType);
            if (blockType == BlockType.SND_CGY_BLOCK) {
                int resId = 0;
                switch (position) {
                    case 0:
                        resId = R.drawable.green_corner;
                        break;
                    case 1:
                        resId = R.drawable.yellow_corner;
                        break;
                    case 2:
                        resId = R.drawable.blue_corner;
                        break;
                }
                ((MainCgyBlock) appView).setBgImage(resId);
            }
            appView.setKeyListener(mKeyListener);
//            appView.setClickListener(mClickListener);
        } else {
            appView = (SkyBaseItemView) convertView;
        }
        appView.setChildrenMode(isChildrenMode);
        appView.update(mAppList.get(position));
        appView.setIndex(position);
        appView.setTurnPage(turnpage);
        mAppItemList.add(appView);
        return appView;
    }

    public void beginLoadIcon() {
//        for (SkyBaseItemView item : mAppItemList)
//        {
//            item.beginLoadIcon();
//        }
    }

    @Override
    public void refreshUI(List<SkyAppBaseData> datas) {
        if (datas == null) {
            datas = new ArrayList<SkyAppBaseData>();
        }
        LogUtils.i("class_cast", "******************Adapter receive dataList(" + datas.size() + ")******************");
        this.mAppList.clear();
        this.mAppList.addAll(datas);
        this.count = mAppList.size();
        mAppItemList.clear();
        LogUtils.d("aaaaaaaaa-------->type is " + type.toString() + " and size is " + count);
        notifyDataSetChanaged();
    }

    public List<SkyBaseItemView> getItemViews(){
        return mAppItemList;
    }

    public boolean audioMatch(String name) {
        for (SkyBaseItemView view : mAppItemList) {
            if (view.getData().getTitle().equals(name)) {
                if (mAudioMatchListener != null) {
                    mAudioMatchListener.onItemAudioMatch(view.getData(), view.getIndex());
                } else {
                    view.onClick(view);
                }
                return true;
            }
        }
        return false;
    }

    public HashMap<String, String[]> getAppListNames(HashMap<String, String[]> old) {
        if (mAppList != null && mAppList.size() > 0) {
            String[] mNames = new String[mAppList.size()];
            for (int i = 0; i < mAppList.size(); i++) {
                old.put(mAppList.get(i).getTitle(), new String[]{mAppList.get(i).getTitle()});
            }
        }
        return old;
    }

    @Override
    public void destory() {
        for (SkyBaseItemView itemView : mAppItemList) {
            itemView.destory();
        }
    }
}
